#include "Register.h"
#include "ui_Register.h"

Register::Register(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Register)
{
    ui->setupUi(this);
    setFixedSize(501,341);
    ui->manRadioButton->setChecked(true);
    initValidator();

}

Register::~Register()
{
    delete ui;
}

void Register::initValidator()
{
    QRegularExpression pwdRegExp("^[a-zA-Z0-9]{1,8}$");// 密码：1-8位，仅包含英文大小写和数字
    QRegularExpressionValidator* pwdValidator = new QRegularExpressionValidator(pwdRegExp,ui->pwdLineEdit);
    ui->pwdLineEdit->setValidator(pwdValidator);
    ui->pwd2LineEdit->setValidator(pwdValidator);
}

void Register::on_registerBtn_clicked()
{
    // 获取到填写的信息
    QString nickname = ui->nicknameLineEdit->text();
    QString gender = ui->manRadioButton->isChecked() ? "男" : "女";
    QString pwd = ui->pwdLineEdit->text();
    QString pwd2 = ui->pwd2LineEdit->text();
    QString safeQ = ui->safeQLineEdit->text();
    QString safeA = ui->safeALineEdit->text();

    // 判断是否已经填写完毕
    if(nickname.isEmpty()){
        QMessageBox::warning(this, "警告", "请填写昵称", QMessageBox::Ok);
        return;
    }
    else if(pwd.isEmpty()){
        QMessageBox::warning(this, "警告", "请设置密码", QMessageBox::Ok);
        return;
    }
    else if(pwd2.isEmpty()){
        QMessageBox::warning(this, "警告", "请确认密码", QMessageBox::Ok);
        return;
    }
    else if(pwd != pwd2){
        QMessageBox::warning(this, "警告", "密码不一致", QMessageBox::Ok);
        return;
    }
    else if(safeQ.isEmpty()){
        QMessageBox::warning(this, "警告", "请设置密保", QMessageBox::Ok);
        return;
    }
    else if(safeA.isEmpty()){
        QMessageBox::warning(this, "警告", "请设置答案", QMessageBox::Ok);
        return;
    }

    // 获取到头像数据并转换成Base64格式打包
    QPixmap avatarPix = ui->avatarLabel->pixmap();
    QImage avatarImage = avatarPix.toImage();
    QByteArray byteArray;
    QBuffer buffer(&byteArray);
    buffer.open(QIODevice::WriteOnly);
    avatarImage.save(&buffer, "PNG");// 以png格式保存图像
    QString base64Avatar = byteArray.toBase64();// 将图像转换为Base64字符串


    // 打包成json数据包
    QJsonObject jsonObj;
    jsonObj["tag"] = "register";
    jsonObj["nickname"] = nickname;
    jsonObj["gender"] = gender;
    jsonObj["pwd"] = pwd;
    jsonObj["safeQ"] = safeQ;
    jsonObj["safeA"] = safeA;
    jsonObj["avatar"] = base64Avatar;  // 将Base64字符串存储到json中

    // 发送信号给主类
    emit registerData(jsonObj);

    // 关闭注册窗口
    close();
}

MyAvatarLabel::MyAvatarLabel(QWidget *parent):QLabel(parent)
{
    // 初始化设置默认头像
    QPixmap pix(":/images/login/defult_avatar.png");
    this->setPixmap(pix.scaled(150,150,Qt::KeepAspectRatioByExpanding,Qt::SmoothTransformation));

    setCursor(Qt::PointingHandCursor);  // 设置鼠标形状为手形
}

void MyAvatarLabel::mousePressEvent(QMouseEvent *event)
{
    // 检查是否是左键点击
    if(event->button() == Qt::LeftButton){
        // 调出文件选择对话框，并只显示图片
        QString avaterPath = QFileDialog::getOpenFileName(this, tr("选择头像"),QDir::homePath(),tr("Images (*.png *.bmp *.jpeg *.jpg)"));
        if(!avaterPath.isEmpty()){
            QPixmap pix;
            if(pix.load(avaterPath)){
                this->setPixmap(pix.scaled(150,150,Qt::KeepAspectRatioByExpanding,Qt::SmoothTransformation));
            }else{
                QMessageBox::warning(this,"警告","您选择的图片有误或无效",QMessageBox::Ok);
            }
        }
    }
}
